package com.resource.animation
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.geom.Point;
	
	/**
	 * 动画类，按enterFrame()函数被调用频率播放动画
	 * TODO 未考虑坐骑
	 */
	public class Animation extends Sprite implements IAnimation
	{
		private var _data:Object = null;
		private var _frameList:Array = null;
		private var _namePos:Point = null;
		private var _position:Point = null;
		private var _hpBar:Point = null;
		private var _ride:Point = null;
		private var _hitRect:Array = null;
		private var _attRect:Array = null;
		private var _sendBullet:Array = null;
		
		private var _currentFrame:int = 0;
		private var _isPlay:Boolean = false;
		
		private var _bitmap:Bitmap = new Bitmap;

		private var _endCallback:Function = null; // 当播放到最后一帧时回调，默认第一个参数是this
		
		public function Animation(data:Object=null, endCallback:Function=null)
		{
			super();
			this.data = data;
			_endCallback = endCallback;
			addChild(_bitmap);
		}

		public function enterFrame():void{
			if(!_isPlay){
				return;
			}
			if(_frameList && _frameList.length > 0){
				_currentFrame = (_currentFrame + 1) % _frameList.length;
				update();
			}else{
				_currentFrame++;
			}
		}
		
		private function update():void{
			if(_frameList && _frameList.length > _currentFrame){
				if(_frameList[_currentFrame].ptr < 0){
					_bitmap.bitmapData = _frameList[_currentFrame].data;
					_bitmap.x = _frameList[_currentFrame].xoff - _position.x;
					_bitmap.y = _frameList[_currentFrame].yoff - _position.y;
				}else{
					_bitmap.bitmapData = _frameList[_frameList[_currentFrame].ptr].data;
					_bitmap.x = _frameList[_frameList[_currentFrame].ptr].xoff - _position.x;
					_bitmap.y = _frameList[_frameList[_currentFrame].ptr].yoff - _position.y;
				}
				
				// 最后一帧
				if(_currentFrame == _frameList.length - 1){
					if(_endCallback){
						_endCallback.apply(null);
					}
				}
			}
		}
		
		public function gotoAndPlay(frame:int):void{
			_currentFrame = frame;
			_isPlay = true;
			update();
		}
		
		public function gotoAndStop(frame:int):void{
			_currentFrame = frame;
			if(_frameList && _frameList.length > 0){
				_currentFrame %= _frameList.length;
			}
			_isPlay = false;
			update();
		}
		
		public function play():void{
			_isPlay = true;
		}
		
		public function stop():void{
			_isPlay = false;
		}

		public function set data(value:Object):void{
			_data = value;
			if(_data){
				_frameList = _data.frameList;
				_namePos = _data.namePos;
				_position = _data.position;
				_hpBar = _data.hpBar;
				_ride = _data.ride;
				_hitRect = _data.hitRect;
				_attRect = _data.attRect;
				_sendBullet = _data.sendBullet;
			}
		}
		
		public function get data():Object{
			return _data;
		}
		
		public function get hpBar():Point
		{
			return _hpBar;
		}

		public function get position():Point
		{
			return _position;
		}

		public function get namePos():Point
		{
			return _namePos;
		}
		
		public function get ride():Point
		{
			return _ride;
		}

		public function get currentFrame():int
		{
			return _currentFrame;
		}

		public function get frameList():Array
		{
			return _frameList;
		}

		public function get attRect():Object{
			if(_attRect){
				return _attRect[_currentFrame];
			}
			return null;
		}
		
		public function get hitRect():Object{
			if(_hitRect){
				return _hitRect[_currentFrame];
			}
			return null;
		}
		
		public function get sendBullet():Object{
			if(_sendBullet){
				return _sendBullet[_currentFrame];
			}
			return null;
		}
		
		public function get endCallback():Function
		{
			return _endCallback;
		}
		
		public function set endCallback(value:Function):void{
			_endCallback = value;
		}
		
		public function get isPlay():Boolean
		{
			return _isPlay;
		}
		
		public function get totalFrames():int{
			if(_frameList){
				return _frameList.length;
			}
			return 0;
		}
	}
}